<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['merch_type']=$this->merch_type;
$this->model('pei')->updayorder();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['merchname'])){
		$params['merchname like']='%'.trim($_RQ['merchname']).'%';
		$return['params']['merchname']=$_RQ['merchname'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=intval($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['status']!=''){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','mid','type','star','salecate','choufee_type','cateid','mobile','realname','status','jointime','accounttime','logo','merchname');
    $orderby='star desc, id desc';
	$total='';
	$return['list']=pdo_getslice('jy_supplier_user',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	foreach($return['list'] as &$v){
		$v['jointime']=date('Y/m/d H:i:s',$v['jointime']);
		//$v['url']=custom_url('jy_supplier/merch//id='.$v['id'],true);
		$v['member']=cfc('core')->member_show($v['mid']);
		$v['catename']=pdo_getcolumn('jy_supplier_category',array('id'=>$v['cateid'],'uniacid'=>$_SESSION['uniacid']),'catename');
		$v['actionurl']='jy_supplier/goods//api_id='.$v['id'];
		$v['addgoodsurl']='jy_supplier/goods/edit/merchid='.$v['id'];
	}
	$return['status']=array('0'=>array('id'=>0,'title'=>'待入驻'),'1'=>array('id'=>1,'title'=>'入驻中'),'2'=>array('id'=>2,'title'=>'暂停中'));
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'供货商管理'),
			array('op'=>'edit','title'=>'添加供货商'),
		),
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]:没有地址，经纬度的商户，在某些情况下（外卖，附近商户等）可能不会显示出来！
		</br>《平台供货商》《独立供货商》的区别
		</br>1、商城的商品，只可以直接绑定《平台供货商》。
		</br>2、《平台供货商》发货由平台发，但是可以查看订单情况，可以结算订单。
		</br>3、《独立供货商》发货自己发，其他权限都有。
		</br>4、《默认抽成模式》：就是按成交价，按比率抽成给平台；《按供应价结算》：就是按成本价（供应价），直接结算给商户，平台扣取差价；
		','list'=>array(
			array('title'=>'商户ID','type'=>'img_string',
			'data'=>array(
				array('title'=>'ID：','name'=>'id'),
				array('title'=>'MID：','name'=>'mid')
				)
			),
			array('name'=>'star','title'=>'星数_Star','type'=>'star','set'=>'star'),
			array('title'=>'商户信息_Merch','type'=>'img_string','img'=>'logo',
			'data'=>array(
				array('title'=>'名称：','name'=>'merchname'),
				array('title'=>'项目：','name'=>'salecate')
				)
			),
			array('name'=>'catename','title'=>'所属分类','type'=>'string'),
			array('name'=>'type','title'=>'商户类型','type'=>'radio','options'=>'merch_type'),
			array('name'=>'choufee_type','title'=>'抽成类型','type'=>'radio','options'=>'choufee_type'),
			//array('name'=>'url','title'=>'入口','type'=>'ewm'),	
			array('name'=>'jointime','title'=>'入驻时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status2'),						
		),'action'=>array(
			array('type'=>'href','op'=>'addgoods','title'=>'绑定商品','show'=>array('type'=>1)),
			array('type'=>'href_item','action'=>'addgoodsurl','title'=>'添加商品_ADD Goods','show'=>array('type'=>0)),
			array('type'=>'href_item','action'=>'actionurl','title'=>'商品管理_Goods'),
		)),
		'search'=>array(
			array('name'=>'merchname','width'=>15,'title'=>'商户名称','type'=>'string'),
			array('name'=>'realname','width'=>15,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'电话','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status'),
		)
	);
	$return['choufee_type']=$this->choufee_type;
	$return['status2']=array('0'=>'待入驻','1'=>'入驻中','2'=>'暂停中');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_supplier_user',array('id'=>$id));
		$item['lat_lng']=$item['lat_lng']?json_decode($item['lat_lng'],true):array('lng'=>'');
		$item['onlinetime']=$item['onlinetime']?json_decode($item['onlinetime'],true):array('end'=>'');
	}else{
		$item=array();
	}
	$choufee_set=$item['choufee_set']?json_decode($item['choufee_set'],true):array('commission_type'=>'0','shopcommission_type'=>'0');
	$choufee_set['rate']=$choufee_set['payrate']?:0;
	$item=array_merge($item,$choufee_set);
	$item['upass']='';
    $return['item']=$item;
	$htmls=array( 
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'供货商设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'供货商管理'),
			array('op'=>'edit','title'=>'添加供货商'),
		),
		'edit'=>array(
			array('name'=>'merchname','title'=>'商户名称','type'=>'string'),
			array('name'=>'logo','title'=>'商户logo','type'=>'image'),
			array('name'=>'type','title'=>'商户类型','type'=>'radio','options'=>'merch_type'),
			array('type'=>'radio','title'=>'抽成方式','name'=>'choufee_type','options'=>'choufee_type','help'=>'《默认抽成模式》：就是按成交价，按比率抽成给平台；《按供应价结算》：就是按成本价（供应价），直接结算给商户，平台扣取差价；'),
			//array('name'=>'mypeisong','title'=>'是否自己发货？','type'=>'switch','help'=>'不开启，则由平台统一发货！'),
			array('name'=>'isbrand','title'=>'属于品牌？','type'=>'switch'),
			array('name'=>'star','title'=>'评星','type'=>'unitstring','unit'=>'星'),
			array('name'=>'sudu','title'=>'速度评星','type'=>'unitstring','unit'=>'星'),
			array('name'=>'salecate','title'=>'主营项目','type'=>'string'),	
			array('name'=>'cateid','title'=>'商户分类','type'=>'select','options'=>'cate','option_title'=>'catename'),
			array('name'=>'realname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'offline','title'=>'休息?','type'=>'switch'),
			array('type'=>'time_hour','title'=>'营业时间','name'=>'onlinetime'),
			array('name'=>'uname','title'=>'账户名','type'=>'string'),
			array('name'=>'upass','title'=>'账户密码','type'=>'string'),
			array('name'=>'mid','title'=>'选择管理员','type'=>'choose','action'=>'get_member'),					
			array('name'=>'desc','title'=>'商户简介','type'=>'text'),
			array('type'=>'text','title'=>'商户公告','name'=>'remark'),
			array('name'=>'address','title'=>'商户地址','type'=>'string'),	
			array('name'=>'lat_lng','title'=>'坐标','type'=>'lat_lng'),
			array('name'=>'tel','title'=>'商户电话','type'=>'string'),
			//array('name'=>'out_pay','title'=>'配送费','type'=>'string'),
			//array('name'=>'out_price','title'=>'起送金额','type'=>'string'),
			//array('name'=>'man_fee','title'=>'满额免配送费','type'=>'string'),
			//array('name'=>'out_juli','title'=>'最大配送距离','type'=>'unitstring','unit'=>'KM','help'=>'留空则由平台统一'),
			//array('name'=>'out_upfee','title'=>'打包费','type'=>'unitstring','unit'=>'元','help'=>'留空则为0！'),
			array('name'=>'isrecommand','title'=>'是否推荐','type'=>'radio','options'=>'status2'),	
			array('name'=>'salenum','title'=>'销量','type'=>'string'),
			array('name'=>'groupid','title'=>'商户组','type'=>'select','options'=>'group','option_title'=>'groupname'),	
			//array('name'=>'payrate','title'=>'抽成利率（%）','type'=>'string'),
			array('type'=>'radio','title'=>'外卖单佣金计算方式','name'=>'commission_type','options'=>'commission_types','help'=>''),
			array('type'=>'checkbox','title'=>'抽成项目','name'=>'dish_feetype','options'=>'dish_feetypes'),
			array('type'=>'checkbox','title'=>'不抽成项目','name'=>'no_feetype','options'=>'no_feetypes'),
			array('type'=>'unitstring','name'=>'rate','unit'=>'%','title'=>'抽成比例','show'=>array('commission_type'=>0),'help'=>'按百分比算：例如5，表示收起5%'),
			array('type'=>'unitstring','name'=>'rateyuan','unit'=>'元','title'=>'固定抽成','show'=>array('commission_type'=>1),'help'=>'按每单固定金额抽成！'),
			array(
							'type'=>'array',
							'name'=>'ratejieti',
							'title'=>'阶梯抽成',
							'show'=>array('commission_type'=>2),
							'add_tip'=>'添加一个阶梯',
								'list'=>array(
									array('type'=>'string','name'=>'fee','title'=>'订单满多少'),
									array('type'=>'string','name'=>'rate','title'=>'抽成%'),
								),
								'details'=>array(
									array('type'=>'unitstring','unit'=>'元','name'=>'fee','title'=>'订单满多少'),
									array('type'=>'unitstring','unit'=>'%','name'=>'rate','title'=>'抽成%'),
								)
			),
			array('type'=>'radio','title'=>'商城单佣金计算方式','name'=>'shopcommission_type','options'=>'commission_types','help'=>''),
			array('type'=>'checkbox','title'=>'抽成项目','name'=>'shop_feetype','options'=>'shop_feetypes'),
			array('type'=>'checkbox','title'=>'不抽成项目','name'=>'shopno_feetype','options'=>'no_feetypes'),
			array('type'=>'unitstring','name'=>'shoprate','unit'=>'%','title'=>'抽成比例','show'=>array('shopcommission_type'=>0),'help'=>'按百分比算：例如5，表示收起5%'),
			array('type'=>'unitstring','name'=>'shoprateyuan','unit'=>'元','title'=>'固定抽成','show'=>array('shopcommission_type'=>1),'help'=>'按每单固定金额抽成！'),
			array(
							'type'=>'array',
							'name'=>'shopratejieti',
							'title'=>'阶梯抽成',
							'show'=>array('shopcommission_type'=>2),
							'add_tip'=>'添加一个阶梯',
								'list'=>array(
									array('type'=>'string','name'=>'fee','title'=>'订单满多少'),
									array('type'=>'string','name'=>'rate','title'=>'抽成%'),
								),
								'details'=>array(
									array('type'=>'unitstring','unit'=>'元','name'=>'fee','title'=>'订单满多少'),
									array('type'=>'unitstring','unit'=>'%','name'=>'rate','title'=>'抽成%'),
								)
			),
			array('name'=>'payopenid','title'=>'选择收款人','type'=>'choose_title','action'=>'get_member'),	
			array('name'=>'content','title'=>'商户详情','type'=>'richtext'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['cate']=pdo_getall('jy_supplier_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	$return['group']=pdo_getall('jy_supplier_group',array('uniacid'=>$_SESSION['uniacid']),array('id','groupname'));
	$return['status']=array('0'=>'待入驻','1'=>'入驻中','2'=>'暂停中');
	$return['status2']=array('1'=>'是','0'=>'否');
	$return['commission_types']=$this->commission_types;
	unset($return['commission_types'][3]);
	$return['dish_feetypes']=$this->dish_feetypes;
	$return['shop_feetypes']=$this->shop_feetypes;
	$return['no_feetypes']=$this->no_feetypes;
	$return['choufee_type']=$this->choufee_type;
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['merchname'] ||  !$post['uname']){
		exi('商户名称和账户必填','error');
	}
	if(!$post['cateid']){
		exi('商户分类必选！','error');
	}
	if(!$post['lat_lng']){
		exi('请填写坐标！','error');
	}
	if($post['star'] >5){
		exi('评星不能大于5！','error');
	}
	if($post['sudu'] >5){
		exi('速度评星不能大于5！','error');
	}
	$infomid=pdo_getcolumn('jy_supplier_user',array('mid'=>$post['mid'],'uniacid'=>$_SESSION['uniacid']),'mid');
	$uname=pdo_get('jy_supplier_user',array('uname'=>$post['uname']));
	$post['choufee_set']=array(
		'commission_type'=>$post['commission_type'],
		'dish_feetype'=>$post['dish_feetype'],
		'no_feetype'=>$post['no_feetype'],
		'rate'=>$post['rate'],
		'rateyuan'=>$post['rateyuan'],
		'ratejieti'=>$post['ratejieti'],
		'shopcommission_type'=>$post['shopcommission_type'],
		'shop_feetype'=>$post['shop_feetype'],
		'shopno_feetype'=>$post['shopno_feetype'],
		'shoprate'=>$post['shoprate'],
		'shoprateyuan'=>$post['shoprateyuan'],
		'shopratejieti'=>$post['shopratejieti'],
	);
	$data =array(
		'uname'=>$post['uname'],
		'upass'=>base64_encode($post['upass']),
		'mobile'=>$post['mobile'],
		'realname'=>$post['realname'],
		'merchname'=>$post['merchname'],
		'offline'=>intval($post['offline']),
		'onlinetime'=>ijson_encode($post['onlinetime']),
		'logo'=>$post['logo'],
		'desc'=>$post['desc'],
		'remark'=>$post['remark'],
		'salecate'=>$post['salecate'],
		'status'=>$post['status'],
		'address'=>$post['address'],
		'lat_lng'=>ijson_encode($post['lat_lng']),
		'mid'=>$post['mid'],
		'cateid'=>intval($post['cateid']),
		'star'=>intval($post['star']),
		'sudu'=>intval($post['sudu']),
		'isrecommand'=>$post['isrecommand'],
		'tel'=>$post['tel'],
		'out_pay'=>$post['out_pay'],
		'out_price'=>$post['out_price'],
		'man_fee'=>$post['man_fee'],
		'salenum'=>$post['salenum'],
		'groupid'=>$post['groupid'],
		'payrate'=>0,
		'payopenid'=>$post['payopenid'],
		'isbrand'=>$post['isbrand'],
		'content'=>trim($post['content']),
		'mypeisong'=>$post['mypeisong'],
		'out_juli'=>$post['out_juli'],
		'out_upfee'=>$post['out_upfee'],
		'choufee_set'=>ijson_encode($post['choufee_set']),
		'type'=>intval($post['type']),
		'choufee_type'=>intval($post['choufee_type']),
	);
	if($post['id']){
		if(!$post['upass']){
			unset($data['upass']);
		}
		if($infomid && $infomid!=$post['mid']){
			exi('一个管理员只能绑定一个账号','error');
		}
		if($uname && $uname['id']!=$post['id']){
			exi('登陆账户名不可以重复','error');	
		}
		pdo_update('jy_supplier_user',$data,array('id'=>$post['id']));
		exi('保存成功','','list');
	}else{
		if($uname){
			exi('登陆账户名不可以重复','error');	
		}
		if($infomid){
			exi('一个管理员只能绑定一个账号','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['jointime']=time();
		pdo_insert('jy_supplier_user',$data);
		exi('添加成功','','list');
	}
	
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_supplier_user',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_supplier_user',array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('jy_supplier_user',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='addgoods'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$status=intval();
		$id=intval($post['id']);
		$update=array('status'=>$status,'ispay'=>intval($post['ispay']));
		if(!$post['goods']){
			exi('请选择商品！','error');
		}
		foreach($post['goods'] as $g){
			pdo_update('jy_weishop_goods',array('api_id'=>$id,'api_pluginname'=>'jy_supplier'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$g['id']));
		}
		exi('设置成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_supplier_user',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','merchname'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'添加商品',
	);
	$htmls['edit']=array(
			array('type'=>'copy','title'=>'当前供货商','name'=>'merchname'),
			array('type'=>'choose_many','title'=>'选择商品','name'=>'goods','action'=>'/get_goods/many/','choose_title'=>'选择绑定的商品'),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
?>